// pages/movie/home/home.js

import {in_theaters, coming_soon, selectPreview} from '../../../utils/request'

var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk');
var qqmapsdk = new QQMapWX({
  key: 'REUBZ-BIQ6F-IHTJA-N5I24-ZXZLV-3TFE7'
});  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: ['banner1.jpg', 'banner2.jpg', 'banner3.jpg'],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    intherList:[],
    comingList:[],
    list:[],
    previewList:[],
    city:''
  },
  tohot(){
    wx.reLaunch({
      url:'../movie/movie?id=0'
    })
  },
  tocom(){
    wx.reLaunch({
      url:'../movie/movie?id=1'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    in_theaters({
      start:3
    })
    .then(res=>{
      console.log(res)
      this.setData({
        intherList:res.data.data.list
      })
    })

    coming_soon({
      start:1
    })
    .then(res=>{
      console.log(res)
      var showlist = []
      for (var i=0;i<res.data.data.list.length;i++){
        showlist.push(res.data.data.list[i].show_date)
      }
      // console.log(showlist)
      var showtimelist = []
      for (var i=0;i<showlist.length;i++){
        showtimelist.push(showlist[i][0])
      }
      // console.log(showtimelist)
      var timeArr = []
      for (var i=0;i<showtimelist.length;i++){
        timeArr.push(showtimelist[i].split('(')[0])
      }
      // console.log(timeArr)

      var datalist = []
      for (var i=0;i<timeArr.length;i++){
        datalist.push(timeArr[i].split('-'))
      }
      // console.log(datalist)

      var arr = []
      for (var i=0;i<datalist.length;i++){
        arr.push(datalist[i].splice(1,2))
      }
      // console.log(arr)
      
      for(var i=0;i<arr.length;i++){
        if(Number(arr[i][0]) < 10){
          arr[i][0] = Number(arr[i][0]) + '月'
        }else{
          arr[i][0] = arr[i][0] + '月'
        }
        if(Number(arr[i][1]) < 10){
          arr[i][1] = Number(arr[i][1]) + '日'
        }else{
          arr[i][1] = arr[i][1] + '日'
        }
      }
      console.log(arr)

      var timelist = []
      for (var i=0;i<arr.length;i++){
        timelist.push(arr[i].join(''))
      }
      console.log(timelist)

      this.setData({
        comingList:res.data.data.list,
        list:timelist
      })
    })

    selectPreview()
    .then(res=>{
      console.log(res)
      this.setData({
        previewList:res.data.data
      })
    })
  },
  getlocation(){
    var that = this
    wx.getLocation({
      success(res){
        console.log(res)// latitude: 34.79977  longitude: 113.66072
        qqmapsdk.reverseGeocoder({
          location:`${res.latitude},${res.longitude}`,
          success(val){
            console.log(val)
            that.setData({
              city:val.result.address_component.city
            })
          }
        })       
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getlocation()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})